package com.koron.standard.deviceClasssify.excel;

import cn.hutool.core.map.BiMap;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.koron.common.core.business.datadict.utils.DataDictTool;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * EAM专用
 * @author jzp
 * @since 2022/5/12
 */
public class YCTypeStringStringConvertor extends StringStringConverter {

    /**
     * {
     *     字段名：{
     *         数据库值：前端显示值
     *     }
     * }
     */
    private final Map<String, BiMap<String, String>> fieldMap = new HashMap<>();
    private final String title = "$title$";
    /**
     * 初始化字典数据
     */
    public YCTypeStringStringConvertor(){
        // 分类类型 isVirtual
        BiMap<String, String> isVirtualMap = new BiMap<>(DataDictTool.getDataDictByCode("standardManagement", "standard_device_type"));
        isVirtualMap.put(title, "分类类型");
        // 专业 major
        // BiMap<String, String> majorMap = new BiMap<>(DataDictTool.getDataDictByCode("publicDictionary", "common_major"));
        BiMap<String, String> majorMap = new BiMap<>(new HashMap<>());
        majorMap.put("machine", "机械");
        majorMap.put("electric", "电气");
        majorMap.put("meter", "仪表");
        majorMap.put("automate", "自控");
        majorMap.put("assay", "化验");
        majorMap.put("special", "特种");
        majorMap.put("security", "安防");
        majorMap.put("communication", "通讯");
        majorMap.put("others", "其它");

        majorMap.put(title, "专业");

        // 数据类型 dataType
        BiMap<String, String> dataTypeMap = new BiMap<>(DataDictTool.getDataDictByCode("standardManagement", "standard_data_type"));
        dataTypeMap.put(title, "数据类型");

        // 输入类型 enterType
        BiMap<String, String> enterTypeMap = new BiMap<>(DataDictTool.getDataDictByCode("standardManagement", "standard_enter_type"));
        enterTypeMap.put(title, "输入类型");

        // 是否必填 isMust
        BiMap<String, String> isMustMap = new BiMap<>(DataDictTool.getDataDictByCode("standardManagement", "standard_is_must"));
        isMustMap.put(title, "是否必填");

        // 基本参数组 groupId
        // TypeGroupService typeGroupService = SpringUtil.getBean(TypeGroupService.class);
        // List<TypeGroup> list = typeGroupService.list();
        // BiMap<String, String> groupIdMap = new BiMap<>(list.stream().collect(Collectors.toMap(TypeGroup::getId, TypeGroup::getName)));
        BiMap<String, String> groupIdMap = new BiMap<>(new HashMap<>());
        groupIdMap.put("46499bb38a5045cb959891ed1c1bc506", "A");
        groupIdMap.put("d623079fafee452fadb4779acb7b7612", "B");
        groupIdMap.put("c6b031332fd94ce8a74583c48c5ede3e", "C");

        groupIdMap.put(title, "基本参数组");

        // 字段和数据字典对应
        fieldMap.put("isVirtual", isVirtualMap);
        fieldMap.put("major", majorMap);
        fieldMap.put("dataType", dataTypeMap);
        fieldMap.put("enterType", enterTypeMap);
        fieldMap.put("isMust", isMustMap);
        fieldMap.put("groupId", groupIdMap);
    }

    @Override
    public String convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty,
                                    GlobalConfiguration globalConfiguration) {
        String key = fieldMap.get(contentProperty.getField().getName()).getKey(cellData.getStringValue());
        if (StrUtil.isNotEmpty(cellData.getStringValue()) && StrUtil.isEmpty(key)) {
            throw new IllegalArgumentException("找不到" + cellData.getStringValue() + "对应的" + fieldMap.get(contentProperty.getField().getName()).get(title));
        }
        return key;
    }

    @Override
    public WriteCellData<String> convertToExcelData(String value, ExcelContentProperty contentProperty,
                                               GlobalConfiguration globalConfiguration) {
        return new WriteCellData<>(Objects.toString(fieldMap.get(contentProperty.getField().getName()).get(value), value));
    }
}
